# coding:utf-8
from django.shortcuts import render
from django.http import HttpResponse
import numpy

# 引入我们创建的表单类
from .forms import AddForm
import csv
import json
#json防错
from django.utils.safestring import SafeString

class CSV():
    def read(self, path_string):
        csv_raw_data = numpy.loadtxt(path_string, dtype = 'string', delimiter = ',', usecols = None)
        return csv_raw_data


def index(request):
    # 当提交表单时
    return render(request, 'index.html')


def result(request):
    if request.method == 'POST':
        faultname = request.POST['fault']
        if faultname == 'EGT_N1_LRRA':
            csv1 = CSV()
            raw_data = csv1.read("EGT_N1_LRRA.csv")
            list_time_1 = []
            for line in raw_data:
                list_time_1.append(line[0])
            list_time = json.dumps(list_time_1)
            return render(request, 'echarts-egt+N1+LRRA.html',{"list_time": list_time})
            #str_result=str(123)
        #else:
            #str_result = firstname
        #data = numpy.loadtxt('fuel_model.csv', dtype = 'string', delimiter = ',', usecols = None)
            #reader = csv.reader(file('fuel_model.csv', 'rb'))
            #pic_name = faultname
            #list_fueldata = reader
            #return render(request, 'query_result.html', {'list_fueldata' : list_fueldata, 'pic_name'：pic_name})

        #if faultname == 'LRRA':
        else:
            reader = csv.reader(file(faultname + '_model.csv', 'rb'))
            pic_name = faultname
            list_fueldata = reader

            return render(request, 'query_result.html', {'list_fueldata' : list_fueldata, 'pic_name':pic_name,})